package ch.hsr.roboProject.RoboCamWifi;

import java.io.File;
import java.io.FileDescriptor;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.hardware.Camera;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		ParcelFileDescriptor pfd = null;
		TextView tv = (TextView) findViewById(R.id.tv);
		Button start = (Button) findViewById(R.id.start);
		Button stop = (Button) findViewById(R.id.stop);
		byte[] adr = { (byte) 152, (byte) 96, (byte) 233, (byte) 63 };
		try {
			Socket soc = new Socket(InetAddress.getByAddress(adr), 8080);
			pfd = ParcelFileDescriptor.fromSocket(soc);

		} catch (UnknownHostException e) {
			tv.setText(e.toString());
		} catch (IOException e) {
			tv.setText(e.toString());
		} catch (SecurityException e) {
			tv.setText(e.toString());
		}

		final MediaRecorder mRecorder = new MediaRecorder();
		// mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
		// mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		// mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
		// mRecorder.setVideoFrameRate(10);
		// mRecorder.setVideoSize(640, 480);
		// mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
		mRecorder.setOutputFile(pfd.getFileDescriptor());
		mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
		mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
		// mRecorder.setOutputFile("/sdcard/temprobo/temp.mp4");
		// Camera c = null;
		// tv.setText("init fertig");
		try {
			// c = Camera.open();
			// Parameters p = c.getParameters();
			// c.unlock();
			// mRecorder.setCamera(c);
		} catch (Exception e) {
			tv.setText(e.toString());
		}

		try {
			mRecorder.prepare();

		} catch (Exception e) {
			tv.setText(e.toString());
		}

		start.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mRecorder.start();
			}
		});

		stop.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mRecorder.stop();
				mRecorder.reset();
				mRecorder.release();
			}
		});
	}
}